<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <el-input v-model="queryInfo.query" prefix-icon="el-icon-search">
            <el-button slot="append" @click="search">搜 索</el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column width="80px" label="订单价格" prop="order_price"></el-table-column>
        <el-table-column width="80px" label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag type="success" v-if=" scope.row.pay_status === '1' ">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="80px" label="是否发货" prop="is_send"></el-table-column>
        <el-table-column width="180px" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column width="140px" label="操作">
          <template slot-scope="scope">
            <el-tooltip content="修改地址" :enterable="false" placement="top">
              <el-button
                @click="addressVisibleClick(scope.row)"
                type="primary"
                size="small"
                icon="el-icon-edit"
                circle
              ></el-button>
            </el-tooltip>
            <el-tooltip content="查看物流信息" :enterable="false" placement="top">
              <el-button @click="progressVisibleClick(scope.row)" type="success" size="small" icon="el-icon-map-location" circle></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <el-dialog title="修改地址" :visible.sync="addressVisible" width="40%" :before-close="handleClose">
      <el-form ref="addressFormRef" :rules="addressFormRules" :model="addressForm">
        <el-form-item prop="address1" label="省市区/县">
         <el-cascader :options="cityData" v-model="addressForm.address1" :props="{ expandTrigger: 'hover' }">
         </el-cascader>
        </el-form-item>
        <el-form-item prop="address2" label="详细地址">
          <el-input v-model="addressForm.address2" type="text"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitAddress">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改地址" :visible.sync="progressVisible" width="60%" :before-close="progressHandleClose">
        <el-timeline>
    <el-timeline-item v-for="(item,i) in progressInfo" :key="i" :timestamp="item.time" placement="top">
      <el-card>
        <h4>{{item.context}}}</h4>
      </el-card>
    </el-timeline-item>
  </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressHandleClose" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import cityData from './citydata'

export default {
  data() {
    return {
      addressForm: {
        address1: [],
        address2: ""
      },
      addressFormRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "blur" }
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "blur" }
        ]
      },
      cityData,
      orderList: [],
      progressInfo:[],
      total: 0,
      addressVisible: false,
      progressVisible:false,
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      }
    };
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo
      });
      console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("获取订单列表失败");

      this.orderList = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    async search() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.orderList = res.data.goods;
      this.total = res.data.total;
      console.log(this.queryInfo.query);
      console.log(res);
    },
    handleClose() {
        this.$refs.addressFormRef.resetFields()
        this.addressVisible = false
    },
    progressHandleClose(){
      this.progressVisible = false
    },
    addressVisibleClick() {
      this.addressVisible = true
    },
     async progressVisibleClick(){
     this.progressVisible = true 
     this.progressInfo = []
     const { data:res } = await this.$http.get('/kuaidi/1106975712662')
     if( res.meta.status !== 200 )
     return this.$message.error(res.meta.msg)
     
     this.progressInfo = res.data
          console.log(this.progressInfo)
    },
    submitAddress(){
        	this.$refs.addressFormRef.validate( async valid => {
					if(!valid){
                        return this.$message.error('请选择地区！')
					}
					    this.addressVisible = false
						return this.$message.success('设置地区成功')
					}
					
				)
    }
  },
  created() {
    this.getOrderList();
  }
};
</script>

<style lang="less" scoped>
.el-table .el-button {
  margin-left: 15px;
}
.el-cascader{
    width: 80%;
}
</style>